//define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'echarts', 'echarts-theme', 'template'], function ($, undefined, Backend, Table, Echarts, Template, Form) {
define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template','bootstrap-daterangepicker'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user_keep_day/index' + location.search,
                    add_url: 'user_keep_day/add',
                    edit_url: 'user_keep_day/edit',
                    del_url: 'user_keep_day/del',
                    multi_url: 'user_keep_day/multi',
                    import_url: 'user_keep_day/import',
                    table: 'user_keep_day',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                       /* {field: 'id', title: __('Id')},*/
                        {field: 'day_register', title: __('日期'), operate:'RANGE', addclass:'datetimerange', data:'data-date-format="YYYY-MM-DD"',autocomplete:false,sortable: true},
                        {field: 'app_id', title: __('应用名'),addclass: 'selectpage', extend:'data-source="ajax/getApp" ',visible:false,defaultValue:'11'},



                        {field: 'app.name', title: __('应用名'), operate:false},
                        {field: 'num', title: __('Num'),sortable: true,operate: false},
                        {field: 'scale2', title: __('次日'),sortable: true,operate: false},
                        {field: 'scale3', title: __('3日'),sortable: true,operate: false},
                        {field: 'scale4', title: __('4日'),sortable: true,operate: false},
                        {field: 'scale5', title: __('5日'),sortable: true,operate: false},
                        {field: 'scale6', title: __('6日'),sortable: true,operate: false},
                        {field: 'scale7', title: __('7日'),sortable: true,operate: false},
                        {field: 'scale15', title: __('15日'),sortable: true,operate: false},
                        {field: 'scale30', title: __('30日'),sortable: true,operate: false},
                        {field: 'scale60', title: __('60日'),sortable: true,operate: false},
                        {field: 'scale90', title: __('90日'),sortable: true,operate: false},
                        {field: 'type', title: __('type'), searchList: {"0":__('新增'),"1":__('活跃')},defaultValue:'0', formatter: Table.api.formatter.status},
                        /*{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}*/
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            /*$(".toggle1").addClass("toggle1 active");
            $(".toggle2").click(function(){
                Backend.api.addtabs('user_keep_day/echarts','折线图');
                //window.location.href = "user_keep_day/echarts?addtabs=1";
            })*/
            //获取到表格里面的所有数据
            table.on('load-success.bs.table',function (e,data){
                legend = data.legend;
                date = data.date;
                console.log(data);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        echarts: function () {
            //时间插件
            require(['bootstrap-datetimepicker'], function () {
                var options = {
                    format: 'YYYY-MM-DD HH:mm:ss',
                    icons: {
                        time: 'fa fa-clock-o',
                        date: 'fa fa-calendar',
                        up: 'fa fa-chevron-up',
                        down: 'fa fa-chevron-down',
                        previous: 'fa fa-chevron-left',
                        next: 'fa fa-chevron-right',
                        today: 'fa fa-history',
                        clear: 'fa fa-trash',
                        close: 'fa fa-remove'
                    },
                    showTodayButton: true,
                    showClose: true
                };
                $('.datetimepicker').parent().css('position', 'relative');
                $('.datetimepicker').datetimepicker(options);
            });
           /* var legend = [];
            var xAxis = [];
            var series = [];*/
            var field = $('#c-flag option:selected').val();
            var startDay = $('input[name="startDay"]').val();
            var endDay = $('input[name="endDay"]').val();
            var type = $('#c-type option:selected').val();
            loadEcharts(startDay, endDay, field);
            $('.btn-warning').click(function (){
                loadEcharts($('input[name="startDay"]').val(), $('input[name="endDay"]').val(), $('#c-flag option:selected').val(), $('#c-type option:selected').val());
            });
            function loadEcharts(startDay, endDay, field, type) {
                $.ajax({
                    async: false,
                    url: 'user_keep_day/echarts',
                    type: 'get',
                    data: {startDay: startDay, field: field, endDay: endDay,type:type},
                    dataType: 'json',
                    success: function (res) {
                        if (res) {
                            /*console.log(res);
                            legend = res.legend;
                            xAxis = res.xAxis;
                            series = res.series;*/
                            var myChart = Echarts.init(document.getElementById('keep'), 'walden');

                            // 指定图表的配置项和数据
                            option = {
                                title: {
                                    text: '用户新增留存率'
                                },
                                tooltip: {
                                    trigger: 'axis'
                                },
                                legend: {
                                    //data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎']
                                    data: res.legend
                                },
                                grid: {
                                    left: '3%',
                                    right: '4%',
                                    bottom: '3%',
                                    containLabel: true
                                },
                                toolbox: {
                                    feature: {
                                        saveAsImage: {}
                                    }
                                },
                                xAxis: {
                                    type: 'category',
                                    boundaryGap: false,
                                    //data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                                    data: res.xAxis
                                },
                                yAxis: {
                                    type: 'value'
                                },
                                series:res.series,
                            };

                            option && myChart.setOption(option);

                            //console.log(series);
                        } else {
                            Toastr.error('服务器繁忙请稍后再试');
                        }
                    }
                });
            }

           /* $(".toggle2").addClass("toggle1 active");
            $(".toggle1").click(function(){
                Backend.api.addtabs('user_keep_day/index','表格');
                //window.location.href = 'user_keep_day/index';
            })*/


            $(window).resize(function () {
                myChart.resize();
            });

            $(document).on("click", ".btn-refresh", function () {
                setTimeout(function () {
                    myChart.resize();
                }, 0);
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                //Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});